﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CommonRepositoryInterface
{
    public interface IDataRecord
    {
        int Id { get; set; }
        DateTime DateCreated { get; set; }
        DateTime DateModified { get; set; }
    }

    public static class IDataRecordEx
    {
        public static T Id<T>(this IEnumerable<T> query, int id)
            where T : IDataRecord
        {
            return (from q in query
                    where q.Id == id
                    select q).FirstOrDefault();
        }
        public static T Id<T>(this IQueryable<T> query, int id)
            where T: IDataRecord
        {
            return (from q in query
                    where q.Id == id
                    select q).FirstOrDefault();
        }
        public static bool IsNew(this IDataRecord record)
        {
            return record.Id == Database.NewRecordId;
        }
        public static T CreateIfNull<T>(this T record)
            where T: IDataRecord, new()
        {
            return new T
            {
                Id = Database.NewRecordId
            };
        }
    }
}
